/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package enteties.article;

import enteties.person.SupplierFacadeLocal;
import java.util.ArrayList;
import java.util.Collection;
import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import util.article.ArticleDetails;
import util.article.EancodeDetails;
import util.article.ImageDetails;
import util.article.ModelDetails;
import util.article.PriceDetails;
import util.article.SizeDetails;

/**
 *
 * @author Joris
 */
@Stateless
public class ArticleFacade extends AbstractFacade<Article> implements ArticleFacadeLocal {
    @PersistenceContext(unitName = "Liebaut-ejbPU")
    private EntityManager em;
    
    @EJB
    private Group1FacadeLocal groupFacade;
    @EJB
    private SupplierFacadeLocal supplierFacade;
    @EJB
    private ModelFacadeLocal modelFacade;
    @EJB
    private PriceFacadeLocal priceFacade;
    @EJB
    private PriceclassFacadeLocal priceClassFacade;
    @EJB
    private StockFacadeLocal stockFacade;
    @EJB
    private EancodeFacadeLocal eancodeFacade;
    @EJB
    private ImageFacadeLocal imageFacade;
    @EJB
    private Size1FacadeLocal sizeFacade;
    

    @Override
    protected EntityManager getEntityManager() {
        return em;
    }

    public ArticleFacade() {
        super(Article.class);
    }
    
    @Override
    public Integer createArticle(String articleNumber, String description, Integer supplierId, Integer groupId){
        Article article = new Article();
        article.setArticleNumber(articleNumber);
        article.setDescription(description);
        article.setGroupid(groupFacade.find(groupId));
        article.setSupplier(supplierFacade.find(supplierId));
        article.setCreated(null);
        article.setUpdated(null);
        article.setImageCollection(new ArrayList<Image>());
        article.setModelCollection(new ArrayList<Model>());
        article.setPriceCollection(new ArrayList<Price>());
        article.setSize1Collection(new ArrayList<Size1>());
        article.setStockCollection(new ArrayList<Stock>());
        article.setEancodeCollection(new ArrayList<Eancode>());
        create(article);
        return article.getId();
    }
    
    @Override
    public ArticleDetails createArticle(ArticleDetails ad){
        Article a = new Article();
        a.setArticleNumber(ad.getArticleNumber());
        a.setDescription(ad.getDescription());
        a.setSupplier(supplierFacade.find(ad.getSupplier().getId()));
        a.setGroupid(groupFacade.find(ad.getGroup().getId()));
        create(a);
        
        for(ModelDetails md:ad.getModelCollection())
            modelFacade.createModel(md, a);
        
        for(SizeDetails sd:ad.getSizeCollection())
            sizeFacade.createSize(sd, a);
        
        for(ImageDetails id:ad.getImageCollection()){
            Model m = null;
            if(id.getModelid() != null)
                m = modelFacade.find(id.getModelid().getId());               
            imageFacade.createImage(id, a, m);
        }
        
        for(EancodeDetails ed:ad.getEancodeCollection()){
            Model m = null;
            if(ed.getModelid() != null)
                m = modelFacade.find(ed.getModelid().getId());
            Size1 s = null;
            if(ed.getSizeid() != null)
                s = sizeFacade.find(ed.getSizeid().getId());
            eancodeFacade.createEancode(ed, a, m, s);
        }
        
        for(PriceDetails pd:ad.getPriceCollection()){
            Model m = null;
            if(pd.getModel() != null)
                m = modelFacade.find(pd.getModel().getId());
            Size1 s = null;
            if(pd.getSize() != null)
                s = sizeFacade.find(pd.getSize().getId());
            priceFacade.createPrice(pd, a, s, m, priceClassFacade.find(pd.getPriceClass().getId()));
        }
        
        return null;
    }
    
    
    
    public void editArticle (Integer id, String articleNumber, String description, Integer supplierId, Integer groupId) {
        Article article = find(id);
        article.setArticleNumber(articleNumber);
        article.setDescription(description);
        article.setSupplier(supplierFacade.find(supplierId));
        article.setGroupid(groupFacade.find(groupId));
        edit(article);
    }
    
    public void removeArticle(Integer id){
        Article article = find(id);
        remove(article);
    }
    
    @Override
    public ArticleDetails convert(Article a){
        if(a == null)
            return null;
        ArticleDetails detail = new ArticleDetails();
        detail.setId(a.getId());
        detail.setArticleNumber(a.getArticleNumber());
        detail.setDescription(a.getDescription());
        detail.setUpdated(a.getUpdated());
        detail.setCreated(a.getCreated());
        detail.setModelCollection(modelFacade.convert(a.getModelCollection()));
        detail.setPriceCollection(priceFacade.convert(a.getPriceCollection()));
        detail.setStockCollection(stockFacade.convert(a.getStockCollection()));
        detail.setGroup(groupFacade.convert(a.getGroupid(), false));
        detail.setSupplier(supplierFacade.convert(a.getSupplier()));
        detail.setImageCollection(imageFacade.convert(a.getImageCollection()));
        detail.setEancodeCollection(eancodeFacade.convert(a.getEancodeCollection()));
        detail.setSizeCollection(sizeFacade.convert(a.getSize1Collection()));
        return detail;
    }
    
    @Override
    public Collection<ArticleDetails> convert (Collection<Article> articles){
        Collection<ArticleDetails> details = new ArrayList<ArticleDetails>();
        for(Article article : articles){
            details.add(convert(article));
        }
        return details;
    }
    
    
}
